package com.ruoyi.cderp.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.cderp.domain.*;
import com.ruoyi.cderp.mapper.TblProductStockMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.cderp.mapper.TblScTransferOrderMapper;
import com.ruoyi.cderp.service.ITblScTransferOrderService;

/**
 * 仓库调拨单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-01
 */
@Service
public class TblScTransferOrderServiceImpl implements ITblScTransferOrderService
{
    @Autowired
    private TblScTransferOrderMapper tblScTransferOrderMapper;
    @Autowired
    private TblProductStockMapper tblProductStockMapper;


    /**
     * 查询仓库调拨单
     *
     * @param id 仓库调拨单主键
     * @return 仓库调拨单
     */
    @Override
    public TblScTransferOrder selectTblScTransferOrderById(String id)
    {
        return tblScTransferOrderMapper.selectTblScTransferOrderById(id);
    }

    /**
     * 查询仓库调拨单列表
     *
     * @param tblScTransferOrder 仓库调拨单
     * @return 仓库调拨单
     */
    @Override
    public List<TblScTransferOrder> selectTblScTransferOrderList(TblScTransferOrder tblScTransferOrder)
    {
        return tblScTransferOrderMapper.selectTblScTransferOrderList(tblScTransferOrder);
    }

    /**
     * 新增仓库调拨单
     *
     * @param tblScTransferOrder 仓库调拨单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTblScTransferOrder(TblScTransferOrder tblScTransferOrder)
    {
        tblScTransferOrder.setCreateTime(DateUtils.getNowDate());
        int rows = tblScTransferOrderMapper.insertTblScTransferOrder(tblScTransferOrder);
        insertTblScTransferOrderDetail(tblScTransferOrder);
        return rows;
    }

    /**
     * 修改仓库调拨单
     *
     * @param tblScTransferOrder 仓库调拨单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTblScTransferOrder(TblScTransferOrder tblScTransferOrder)
    {
        //收货时修改对应的商品库存
        if(tblScTransferOrder.getStatus()==12){
            for (TblScTransferOrderDetail tblScTransferOrderDetail : tblScTransferOrder.getTblScTransferOrderDetailList()) {
                //转出仓库id
                tblScTransferOrderDetail.getBaseDataProduct().getTblProductStock().setScId(tblScTransferOrder.getSourceScId());
                //转出库存数量
                long stockNum = tblScTransferOrderDetail.getBaseDataProduct().getTblProductStock().getStockNum()-tblScTransferOrderDetail.getTransferNum();
                tblScTransferOrderDetail.getBaseDataProduct().getTblProductStock().setStockNum(stockNum);
                //转出库存含税价格
                BigDecimal taxAmount = BigDecimal.valueOf(stockNum).multiply(tblScTransferOrderDetail.getBaseDataProduct().getTblProductStock().getTaxPrice());
                tblScTransferOrderDetail.getBaseDataProduct().getTblProductStock().setTaxAmount(taxAmount);
                //修改转出商品库存
                tblProductStockMapper.updateTblProductStock(tblScTransferOrderDetail.getBaseDataProduct().getTblProductStock());
                List<TblProductStock> list = tblProductStockMapper.selectTblProductStockList(null);
                //新增转入id
                tblScTransferOrderDetail.getBaseDataProduct().getTblProductStock().setId(String.valueOf((Long.parseLong(list.get(list.size()-1).getId())+1)));
                //转入仓库id
                tblScTransferOrderDetail.getBaseDataProduct().getTblProductStock().setScId(tblScTransferOrder.getTargetScId());
                //根据仓库id和商品id查询仓库中是存在该商品的库存
                TblProductStock stock = tblProductStockMapper.selectTblProductStockByScIdAndProductId(tblScTransferOrderDetail.getBaseDataProduct().getTblProductStock());
                if (stock != null){
                    //转入库存数量
                    long stockNum2 = stock.getStockNum()+tblScTransferOrderDetail.getTransferNum();
                    stock.setStockNum(stockNum2);
                    //转入库存含税价格
                    BigDecimal taxAmount2 = BigDecimal.valueOf(stockNum2).multiply(stock.getTaxPrice());
                    stock.setTaxAmount(taxAmount2);
                    //修改转入商品库存,如果转入的仓库没有对应商品则新增
                    tblProductStockMapper.updateTblProductStock(stock);
                }else{
                    tblScTransferOrderDetail.getBaseDataProduct().getTblProductStock().setStockNum(tblScTransferOrderDetail.getTransferNum());
                    tblScTransferOrderDetail.getBaseDataProduct().getTblProductStock().setTaxAmount(BigDecimal.valueOf(tblScTransferOrderDetail.getTransferNum()).multiply(tblScTransferOrderDetail.getBaseDataProduct().getTblProductStock().getTaxPrice()));
                    tblProductStockMapper.insertTblProductStock(tblScTransferOrderDetail.getBaseDataProduct().getTblProductStock());
                }
            }
        }
        tblScTransferOrder.setUpdateTime(DateUtils.getNowDate());
        tblScTransferOrderMapper.deleteTblScTransferOrderDetailByOrderId(tblScTransferOrder.getId());
        insertTblScTransferOrderDetail(tblScTransferOrder);
        return tblScTransferOrderMapper.updateTblScTransferOrder(tblScTransferOrder);
    }

    /**
     * 批量删除仓库调拨单
     *
     * @param ids 需要删除的仓库调拨单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTblScTransferOrderByIds(String[] ids)
    {
        tblScTransferOrderMapper.deleteTblScTransferOrderDetailByOrderIds(ids);
        return tblScTransferOrderMapper.deleteTblScTransferOrderByIds(ids);
    }

    /**
     * 删除仓库调拨单信息
     *
     * @param id 仓库调拨单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTblScTransferOrderById(String id)
    {
        tblScTransferOrderMapper.deleteTblScTransferOrderDetailByOrderId(id);
        return tblScTransferOrderMapper.deleteTblScTransferOrderById(id);
    }


    /**
     * 新增仓库调拨单明细信息
     *
     * @param tblScTransferOrder 仓库调拨单对象
     */
    public void insertTblScTransferOrderDetail(TblScTransferOrder tblScTransferOrder)
    {
        List<TblScTransferOrderDetail> tblScTransferOrderDetailList = tblScTransferOrder.getTblScTransferOrderDetailList();
        String id = tblScTransferOrder.getId();
        if (StringUtils.isNotNull(tblScTransferOrderDetailList))
        {
            List<TblScTransferOrderDetail> list = new ArrayList<TblScTransferOrderDetail>();
            for (TblScTransferOrderDetail tblScTransferOrderDetail : tblScTransferOrderDetailList)
            {
                tblScTransferOrderDetail.setOrderId(id);
                list.add(tblScTransferOrderDetail);
            }
            if (list.size() > 0)
            {
                tblScTransferOrderMapper.batchTblScTransferOrderDetail(list);
            }
        }
    }
}
